﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace SnakeServer
{
    class Program
    {
        static void Main(string[] args)
        {
            string connStr = "Database=snake;datasource=127.0.0.1;port=3306;user=root;pwd=1346520wbn;SslMode = none;";
            MySqlConnection connection = new MySqlConnection(connStr);
            connection.Open();
            Console.WriteLine("打开数据库");

            HttpListener httpListener = new HttpListener();
            httpListener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
            httpListener.Prefixes.Add("http://45.251.242.42:8080/");
            httpListener.Start();
            Console.WriteLine("Snake Server Started.");
            new Thread(new ThreadStart(delegate
            {
                while (true)
                {
                    HttpListenerContext httpListenerContext = httpListener.GetContext();
                    httpListenerContext.Response.StatusCode = 200;
                    var action = httpListenerContext.Request.QueryString["snake"];
                    //FileInfo barrierfile = new FileInfo(@"barriers\" + barriername + ".skb");
                    switch (action)
                    {
                        case "regist":
                            {
                                string username, password;
                                using (StreamReader sreader = new StreamReader(httpListenerContext.Request.InputStream))
                                {
                                    string str = sreader.ReadToEnd();
                                    var vs = str.Split('%');
                                    username = vs[0];
                                    Console.WriteLine(username);
                                    password = vs[1];
                                    Console.WriteLine(password);
                                    sreader.Close();
                                }
                                string result = DataHandler.RegistAccount(username, password);
                                using (StreamWriter writer = new StreamWriter(httpListenerContext.Response.OutputStream))
                                {
                                    writer.Write(result);
                                    writer.Flush();
                                    writer.Close();
                                }
                                break;
                            }
                        case "upload":
                            {
                                string name, author, json;
                                int dif;
                                using (StreamReader reader = new StreamReader(httpListenerContext.Request.InputStream))
                                {
                                    string str = reader.ReadToEnd();
                                    var vs = str.Split('%');
                                    name = vs[0];
                                    author = vs[1];
                                    dif = int.Parse(vs[2]);
                                    json = vs[3];
                                }
                                var result = DataHandler.UpLoadBarrier(name, author, dif, json);
                                using (StreamWriter writer = new StreamWriter(httpListenerContext.Response.OutputStream))
                                {
                                    writer.Write(result);
                                    writer.Flush();
                                    writer.Close();
                                }
                                break;
                            }
                        case "getcounts":
                            {
                                var result = DataHandler.GetBarrierCount();
                                using (StreamWriter writer = new StreamWriter(httpListenerContext.Response.OutputStream))
                                {
                                    writer.Write(result);
                                    writer.Flush();
                                    writer.Close();
                                }
                                break;
                            }
                        case "getbarrier":
                            {
                                int id;
                                using (StreamReader reader = new StreamReader(httpListenerContext.Request.InputStream))
                                {
                                    id = int.Parse(reader.ReadToEnd());
                                    reader.Close();
                                }
                                var result = DataHandler.ProvideBarrier(id);
                                using (StreamWriter writer = new StreamWriter(httpListenerContext.Response.OutputStream,Encoding.Unicode))
                                {
                                    writer.Write(result);
                                    writer.Flush();
                                    writer.Close();
                                }
                                break;
                            }
                        case "login":
                            {
                                string username, password;
                                using (StreamReader sreader = new StreamReader(httpListenerContext.Request.InputStream))
                                {
                                    string str = sreader.ReadToEnd();
                                    var vs = str.Split('%');
                                    username = vs[0];
                                    password = vs[1];
                                    sreader.Close();
                                }
                                string result = DataHandler.Login(username, password);
                                using (StreamWriter writer = new StreamWriter(httpListenerContext.Response.OutputStream))
                                {
                                    writer.Write(result);
                                    writer.Flush();
                                    writer.Close();
                                }
                                break;
                            }
                        case "uploadranking":
                            {
                                int bid, uid, score, len;
                                using(StreamReader reader=new StreamReader(httpListenerContext.Request.InputStream))
                                {
                                    string str = reader.ReadToEnd();
                                    var vs = str.Split('%');
                                    bid = int.Parse(vs[0]);
                                    uid = int.Parse(vs[1]);
                                    score = int.Parse(vs[2]);
                                    len = int.Parse(vs[3]);
                                }
                                var result = DataHandler.SetRanking(bid, uid, score, len);
                                using (StreamWriter writer = new StreamWriter(httpListenerContext.Response.OutputStream))
                                {
                                    writer.Write(result);
                                    writer.Flush();
                                    writer.Close();
                                }
                                break;
                            }
                        case "getuserid":
                            {
                                string username;
                                using (StreamReader reader = new StreamReader(httpListenerContext.Request.InputStream))
                                {
                                    username = reader.ReadToEnd();
                                    reader.Close();
                                }
                                var result = DataHandler.ReturnUserid(username);
                                using (StreamWriter writer = new StreamWriter(httpListenerContext.Response.OutputStream))
                                {
                                    writer.Write(result);
                                    writer.Flush();
                                    writer.Close();
                                }
                                break;
                            }
                        case "getranking":
                            {
                                int bid;
                                using (StreamReader reader=new StreamReader(httpListenerContext.Request.InputStream))
                                {
                                    string r = reader.ReadToEnd();
                                    bid = int.Parse(r);
                                    reader.Close();
                                }
                                var result = DataHandler.ReadRanking(bid);
                                using (StreamWriter writer = new StreamWriter(httpListenerContext.Response.OutputStream))
                                {
                                    writer.Write(result);
                                    writer.Flush();
                                    writer.Close();
                                }
                                break;
                            }
                        case "getbarrierlist":
                            {
                                var result = DataHandler.ReadBarrierList();
                                using (StreamWriter writer = new StreamWriter(httpListenerContext.Response.OutputStream))
                                {
                                    writer.Write(result);
                                    writer.Flush();
                                    writer.Close();
                                }
                                break;
                            }
                        case "getbarrierinfo":
                            {
                                int bid;
                                using (StreamReader reader=new StreamReader(httpListenerContext.Request.InputStream))
                                {
                                    bid = int.Parse(reader.ReadToEnd());
                                    reader.Close();
                                }
                                var result = DataHandler.ReadBarrierInfo(bid);
                                using (StreamWriter writer = new StreamWriter(httpListenerContext.Response.OutputStream))
                                {
                                    writer.Write(result);
                                    writer.Flush();
                                    writer.Close();
                                }
                                break;
                            }
                        case "getusername":
                            {
                                int uid;
                                using (StreamReader reader = new StreamReader(httpListenerContext.Request.InputStream))
                                {
                                    uid = int.Parse(reader.ReadToEnd());
                                    reader.Close();
                                }
                                var result = DataHandler.ReturnUsername(uid);
                                using (StreamWriter writer = new StreamWriter(httpListenerContext.Response.OutputStream))
                                {
                                    writer.Write(result);
                                    writer.Flush();
                                    writer.Close();
                                }
                                break;
                            }
                    }
                }
            })).Start();
        }

    }
}
